package com.yjlove.myplay.ui.activity;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;
import com.yjlove.myplay.bean.BannerInfo;
import com.yjlove.myplay.ui.adapter.BannerAdapter;
import com.yjlove.myplay.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2017/3/3.
 */
public class BannerTestActivity extends BaseActivity {

    private static final int VIEWPAGERCHANGE = 501;
    private ViewPager mBannerViewPager;
    private TextView mBannreTitle;
    private LinearLayout mBannerDots;

    private List<BannerInfo> mBannerInfos = new ArrayList<>();
    private BannerAdapter mBannerAdapter;
    private List<View> mDots = new ArrayList<>();
    private View mCurrentDot;

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_bantest;
    }

    @Override
    protected void initView() {
        mBannerViewPager = (ViewPager) findViewById(R.id.banner_viewpager);
        mBannreTitle = (TextView) findViewById(R.id.banner_title);
        mBannerDots = (LinearLayout) findViewById(R.id.banner_dots);
    }

    @Override
    protected void initData() {
        mBannerInfos.add(new BannerInfo(R.drawable.banner01, "标题1"));
        mBannerInfos.add(new BannerInfo(R.drawable.banner02, "标题2"));
        mBannerInfos.add(new BannerInfo(R.drawable.banner03, "标题3"));

        int currentPosition = mBannerInfos.size() * 1000 * 100 / 2;
        for (int i=0; i<mBannerInfos.size(); i++){
            createBannerDot();
        }

        mBannerAdapter = new BannerAdapter(BannerTestActivity.this, mBannerInfos);
        mBannerViewPager.setOnPageChangeListener(onPageChangeListener);
        setBannerTitleAndDot(0);
        mBannerViewPager.setAdapter(mBannerAdapter);
        mBannerViewPager.setCurrentItem(currentPosition);
    }

    private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setBannerTitleAndDot(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            //当手动滑动的时候,停止自动滑动
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                handler.sendEmptyMessageDelayed(VIEWPAGERCHANGE, 2000);
            }else{
                handler.removeMessages(VIEWPAGERCHANGE);
            }
        }
    };

    private void setBannerTitleAndDot(int position) {
        position = position % mBannerInfos.size();
        mBannreTitle.setText(mBannerInfos.get(position).getTitle());
        if (mCurrentDot != null){
            mCurrentDot.setSelected(false);
        }
        mDots.get(position).setSelected(true);
        mCurrentDot = mDots.get(position);
    }

    private void createBannerDot() {
        int dp10 = UiUtils.dp2px(10);
        View dot = new View(this);
        //LayoutParams : 代码设置控件的属性
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp10, dp10);
        //背景
        dot.setBackgroundResource(R.drawable.selector_dot_bg);
        //距离右边的距离,5 : px
        params.rightMargin = dp10;
        //将属性设置给点,使点生效
        dot.setLayoutParams(params);//将设置好的属性,设置控件进行显示生效
        //将创建的点,存放到文本的下部显示
        mDots.add(dot);
        mBannerDots.addView(dot);
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            //界面切换
            swicthPage();
        }
    };
    protected void swicthPage() {
        //获取当前显示的界面
        int currentItem = mBannerViewPager.getCurrentItem();//获取当前显示的界面的索引
        //计算下一个界面的索引
        currentItem++;
        //设置viewpager显示下一个界面
        mBannerViewPager.setCurrentItem(currentItem);
        //开始下一个切换操作
        handler.sendEmptyMessageDelayed(VIEWPAGERCHANGE, 2000);
    }
    @Override
    protected void onStart() {
        //当界面可见的时候,重新进行切换
        handler.sendEmptyMessageDelayed(VIEWPAGERCHANGE, 2000);//隔多长时间,给handler发送一个消息
        super.onStart();
    }
    @Override
    protected void onStop() {
        //当界面不可以见的时候,停止切换
        handler.removeMessages(VIEWPAGERCHANGE);//停止给handler发送消息
        super.onStop();
    }
}
